package com.mm.dds.datasource;

import com.alibaba.druid.pool.DruidDataSource;

import java.util.Map;


public class SetDynamicDataSource {

    public static void setDDS(String dbname) {

        //看数据源是否已经生成
        if (!DataSourceContextHolder.isContainsDataSource(dbname)) {

            DruidDataSource dynamicDataSource = new DruidDataSource();
            dynamicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dynamicDataSource.setUrl("jdbc:mysql://192.168.1.107:3306/" + dbname + "?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false");
            dynamicDataSource.setUsername("root");
            dynamicDataSource.setPassword("root");

            /**
             * 创建动态数据源
             */
            String dynamicDS = dbname;
            Map<Object, Object> dataSourceMap = DynamicDataSource.getInstance().getDataSourceMap();
            dataSourceMap.put(dbname, dynamicDataSource);
            DynamicDataSource.getInstance().setTargetDataSources(dataSourceMap);
            DataSourceContextHolder.dataSourceIds.add(dbname);
        }
        DataSourceContextHolder.setDBType(dbname);

    }

}
